#!/bin/bash

# 简化的网易云音乐API Docker部署脚本

set -e

# 颜色输出
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查Docker
check_docker() {
    if ! command -v docker &> /dev/null; then
        log_error "Docker未安装，请先安装Docker"
        exit 1
    fi
    
    if ! command -v docker-compose &> /dev/null; then
        log_error "Docker Compose未安装，请先安装Docker Compose"
        exit 1
    fi
    
    log_info "Docker环境检查通过"
}

# 启动服务
start() {
    log_info "启动网易云音乐API服务..."
    docker-compose -f docker-compose.simple.yml up -d --build
    log_info "服务启动完成！"
    log_info "API地址: http://localhost:3000"
    log_info "测试API: curl http://localhost:3000/banner"
}

# 停止服务
stop() {
    log_info "停止服务..."
    docker-compose -f docker-compose.simple.yml down
    log_info "服务已停止"
}

# 重启服务
restart() {
    log_info "重启服务..."
    stop
    sleep 2
    start
}

# 查看日志
logs() {
    log_info "查看服务日志..."
    docker-compose -f docker-compose.simple.yml logs -f
}

# 查看状态
status() {
    log_info "服务状态:"
    docker-compose -f docker-compose.simple.yml ps
}

# 显示帮助
help() {
    echo "简化的网易云音乐API Docker部署脚本"
    echo ""
    echo "使用方法: $0 [命令]"
    echo ""
    echo "命令:"
    echo "  start    启动服务"
    echo "  stop     停止服务"
    echo "  restart  重启服务"
    echo "  logs     查看日志"
    echo "  status   查看状态"
    echo "  help     显示帮助"
    echo ""
    echo "示例:"
    echo "  $0 start   # 启动服务"
    echo "  $0 logs    # 查看日志"
}

# 主函数
case "${1:-help}" in
    start)
        check_docker
        start
        ;;
    stop)
        stop
        ;;
    restart)
        check_docker
        restart
        ;;
    logs)
        logs
        ;;
    status)
        status
        ;;
    help|--help|-h)
        help
        ;;
    *)
        log_error "未知命令: $1"
        help
        exit 1
        ;;
esac 